package com.uuzz.lottery.agent.gateway.flow.answer.mappingimpl.one2n2nimpl.business.impl;

import java.util.Map;

import javax.annotation.PostConstruct;

import com.uuzz.lottery.agent.gateway.base.AbstractGroupCall;
import com.uuzz.lottery.agent.gateway.base.IConvertor;
import com.uuzz.lottery.agent.gateway.base.ISupplier;
import com.uuzz.lottery.agent.gateway.flow.answer.mappingimpl.one2n2nimpl.business.GroupBusinessIn;
import com.uuzz.lottery.agent.gateway.flow.answer.mappingimpl.one2n2nimpl.business.GroupBusinessOut;
import com.uuzz.lottery.agent.gateway.flow.answer.mappingimpl.one2n2nimpl.business.IBusinessIn;
import com.uuzz.lottery.agent.gateway.flow.answer.mappingimpl.one2n2nimpl.business.IBusinessOut;
import com.uuzz.lottery.agent.gateway.flow.answer.mappingimpl.one2n2nimpl.business.IConnectBusiness;
import com.uuzz.lottery.agent.gateway.flow.answer.mappingimpl.one2n2nimpl.business.IGroupBusiness;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class GroupBusinessImpl<BI extends IBusinessIn,BO extends IBusinessOut,SI extends ISupplier<GroupBusinessIn,BI>,CO extends IConvertor<BO,GroupBusinessOut>,
C extends IConnectBusiness<BI, BO, ?, ?, ?, ?, ?>,CB extends GroupBusinessCallBodySupport<BI,BO,SI, CO, C>>  extends AbstractGroupCall<GroupBusinessIn, GroupBusinessOut, SI, CO, C,CB>
implements IGroupBusiness<SI, CO, C,CB>,ApplicationContextAware
{
	private ApplicationContext applicationContext;
	
	@SuppressWarnings("unchecked")
	@PostConstruct
	public void init(){
		Map<String,CB> map=(Map<String, CB>) applicationContext.getBeansOfType(GroupBusinessCallBodySupport.class);
		for(CB gbcb :map.values()){
			this.add(gbcb);
		}
	}
	
	@Override
	public GroupBusinessOut defaultOut(GroupBusinessIn in) {
		// TODO 这里可以根据入参头信息初始化出参头信息
		return new GroupBusinessOut();
	}

	@Override
	protected void doCall(GroupBusinessIn in, GroupBusinessOut out) {
		String callId = in.getCallId();
		CB callBody = callMaps.get(callId);
		C call = callBody.getCall();
		SI inSupplier = callBody.getInSupplier();
		CO outConvertor = callBody.getOutConvertor();
		BI businessIn =  inSupplier.buildFrom(in);
		BO businessOut   =  call.defaultOut(businessIn);
	    call.call(businessIn, businessOut);
        outConvertor.convert(businessOut, out);		
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext=applicationContext;		
	}
}
